package top.yoahh.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象
 */

@Data
public class Result<T> implements Serializable {
    /**
     * 通信数据
     */
    private T data;
    /**
     * 通信描述
     */
    private String msg = "操作成功";
    /**
     * 通讯代码
     */
    private int code = 200;

    private Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    private Result(T data, int code) {
        this.data = data;
        this.code = code;
    }

    private Result(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public Result(ResultCode code, T data) {
        this.data = data;
        this.msg = code.getMsg();
        this.code = code.getCode();
    }

    public Result(ResultCode code) {
        this.data = data;
        this.msg = code.getMsg();
        this.code = code.getCode();
    }

    public static Result ok() {
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }


    public static Result ok(Integer code,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public static Result fail(Integer code,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result();
        result.setCode(202);
        result.setMsg(msg);
        return result;
    }




}
